#include "usart.h"
void USART2_init(void)
{
    crm_periph_clock_enable(CRM_USART1_PERIPH_CLOCK, TRUE);
    nvic_irq_enable(USART2_IRQn, 1,3);
    usart_init(USART2,115200,USART_DATA_8BITS, USART_STOP_1_BIT);
    usart_interrupt_enable(USART2, USART_IDLE_INT, TRUE);
    usart_interrupt_enable(USART2, USART_RDBF_INT, TRUE);
    usart_flag_clear(USART2, USART_RDBF_FLAG);
    usart_flag_clear(USART2, USART_IDLEF_FLAG);
    usart_enable(USART2, TRUE);
}

void USART2_IRQHandler(void)
{   
    static char input_buf[50];
    static unsigned char current_pos = 0;
    if(usart_flag_get(USART2, USART_RDBF_FLAG) != FALSE)
    {
        usart_flag_clear(USART2, USART_RDBF_FLAG);
        input_buf[current_pos] = usart_data_receive(USART2);
        ++current_pos;
    }
    if(usart_flag_get(USART2, USART_RDBF_FLAG) != FALSE)
    {
        usart_flag_clear(USART2, USART_IDLEF_FLAG);
        input_buf[current_pos] = '\0';
        current_pos = 0;
        cx_debug_handler(input_buf);
    }
}
